<?php	//header
include_once('config/config.php');
require_once "php/functions.php";
$conn = mysql_connect($cfg["connection"]["host"], $cfg["connection"]["userName"], $cfg["connection"]["password"]) 
	or die ('Error Cannot Connect to MySQL');
@mysql_select_db($cfg["connection"]["database"]) or die( "Unable to select database");

require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_bar.php');
require_once ('jpgraph/jpgraph_line.php');

//if (session_status() == PHP_SESSION_NONE) {
    session_start();
//}
$id = $_SESSION['barcode_visualize'];
$tbl_name = db_escape_string($cfg["tbl"]["transaction"]);
$column_name = db_escape_string($cfg["tbl_transaction"]["shopID"]);
/*$cmd = "SELECT SUM(unit_sold) as sales FROM local_transaction WHERE itemId = $id GROUP BY tDate;";
$result = mysql_query($cmd);
$datay = array();
while ($item = mysql_fetch_assoc($result, MYSQL_NUM)) $datay[] = $item[0];*/
$datay = array();
$datax = array();
$datay_count=0;
$datay_mean = 0.0;
for ($i = 1; $i <= 31; $i ++) {
	$dateStr = strtotime("2013-09-".$i."");
	$dateFormat = date('Y-m-d', $dateStr);
	$cmd = "SELECT SUM(unit_sold) as sales FROM $tbl_name WHERE itemId = $id AND tDate = \"".$dateFormat."\";";
//	$result = mysql_query($cmd) or die (mysql_error());
	$total_sales = mysql_fetch_array(mysql_query($cmd));
	$total_sales = $total_sales['sales'];	
	if ($total_sales) array_push($datay, $total_sales);
	else array_push($datay, 0);
	if ($i!=30) {
		$datay_count++;
		$datay_mean=($datay_mean*($datay_count-1) + $datay[$i-1])/$datay_count;
	}
	array_push($datax, $i);
}
//$datay_count*=12; //weightage
//$datay_mean*=12;
$datay2 = array();
$datax2 = array();

$cmd = "SELECT price FROM item WHERE itemId = $id";
$orig_price = mysql_fetch_array(mysql_query($cmd));
array_push($datay2, $orig_price[0]);
array_push($datax2, .9);
$surplus = 0.05;
for ($i = 1; $i <= 30; $i++) {
/*	//REMOVED: hard-code day 30 price as dataset's price for demonstrative purpose
	if ($i==29) {	
		array_push($datay2, $orig_price[0]);
		array_push($datax2, $i+.9);
		continue;
	}
*/
	$datay_count++;
	$temp = ($datay_mean*($datay_count-1) + $datay[$i-1]) / $datay_count;

	if ($temp > $datay_mean) {	//this means We sold stocks ==> Having less stocks
		$temp = abs($datay_mean - $temp)/$datay_mean;
		//$temp = $orig_price[0] + ($temp*$orig_price[0]);
		if ($temp < $surplus) $temp = $datay2[$i-1] + ($temp*$datay2[$i-1]);	//price goes up
		else {
			$temp = $datay2[$i-1]*(1+$surplus);
			if ($surplus < .2) $surplus+=.05;
		}
	}
	else {
		$temp = abs($datay_mean - $temp)/$datay_mean;
		//$temp = $orig_price[0] - ($temp*$orig_price[0]);
		if ($temp < .2) $temp = $datay2[$i-1] - ($temp*$datay2[$i-1]);	//price goes down
		else {
			$temp = $datay2[$i-1]*(1-$surplus);
			if ($surplus < .2) $surplus+=.05;
		}
	}
	
	array_push($datay2, $temp);
	array_push($datax2, $i+.9);
}
$sum = 0;
for ($i = 0; $i<=30; $i++) $sum += $datay2[$i];
$price_estimator = $sum/31;
$_SESSION['price_estimator'] = $price_estimator;

//array_splice($datay2, 0, 1);
//var_dump($datay2);
//array_splice($datax2, 0, 1);
//var_dump($datax2);
//$datay=array(12,26,9,17,31);
$targ = array();
$alts = array();
$alts2 = array();
for ($i = 0; $i < 30; $i++) {
	array_push($targ, curPageURL()."#".$i);
	array_push($alts, "val=%d");
	array_push($alts2, "val=%.2f");
}
// Create the graph. 
// One minute timeout for the cached image
// INLINE_NO means don't stream it back to the browser.
$graph = new Graph(600,480,'auto');
$graph->legend->SetPos(0.5,0.98,'center','bottom');
$graph->SetScale("intlin");
$graph->img->SetMargin(60,30,20,40);
$graph->SetY2Scale("lin");
$graph->yaxis->SetTitleMargin(45);
$graph->yaxis->scale->SetGrace(30);
$graph->y2axis->SetTitleMargin(45);
$graph->y2axis->scale->SetGrace(15);
$graph->SetShadow();

// Turn the tickmarks
$graph->xaxis->SetTickSide(SIDE_DOWN);
$graph->yaxis->SetTickSide(SIDE_LEFT);
$graph->y2axis->SetTickSide(SIDE_RIGHT);
//$graph->xaxis->SetLabelAngle(30);
// Create a bar pot
$bplot = new BarPlot($datay, $datax);
$graph->Add($bplot);

// Create targets for the image maps. One for each column
//$targ=array("","","","","","");
//$alts=array("val=%d","val=%d","val=%d","val=%d","val=%d","val=%d");
$bplot->SetCSIMTargets($targ,$alts);
$bplot->SetLegend("Stocks Sold");
$bplot->SetFillColor("green");

// Use a shadow on the bar graphs (just use the default settings)
$bplot->SetShadow($aColor="black",$aHSize=2,$aVSize=1,$aShow=true);
$bplot->value->SetFormat(" $ %2.1f",70);
$bplot->value->SetFont(FF_ARIAL,FS_NORMAL,10);
$bplot->value->SetColor("blue");

// Create filled line plot
$lplot = new LinePlot($datay2, $datax2);

$graph->AddY2($lplot);
$graph->y2axis->SetColor('blue');
$lplot->value->Show();
$lplot->SetCSIMTargets($targ,$alts2);
$lplot->SetLegend("Price");

$lplot->SetFillColor('skyblue@0.5');
$lplot->SetColor('navy@0.7');
$lplot->SetBarCenter();

$lplot->mark->SetType(MARK_SQUARE);
$lplot->mark->SetColor('blue@0.5');
$lplot->mark->SetFillColor('lightblue');
$lplot->mark->SetSize(6);
//$graph->title->Set("Summary of last month ");
//$graph->xaxis->title->Set("Date");
//$graph->yaxis->title->Set("Sales");
$graph->SetY2OrderBack(false);
$graph->xaxis->SetTitle("Date", "middle");
$graph->y2axis->SetTitle("Price", "middle");
$graph->yaxis->SetTitle("Sales", "middle");


$graph->title->SetFont(FF_FONT1,FS_BOLD);
$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->y2axis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
// Send back the HTML page which will call this script again
// to retrieve the image.
$graph->StrokeCSIMImage();
?>
